#!/bin/bash

# http://greybeardedgeek.net/?p=45 D-BUS version in comment

#sess=`qdbus org.kde.yakuake /yakuake/sessions org.kde.yakuake.activeSessionId`
#qdbus org.kde.yakuake /yakuake/sessions org.kde.yakuake.runCommand ‘cd /opt/tomcat && bin/startup.sh’
#qdbus org.kde.yakuake /yakuake/sessions org.kde.yakuake.splitSessionLeftRight $sess
#qdbus org.kde.yakuake /yakuake/sessions org.kde.yakuake.runCommand ‘cd /opt/tomcat && tail -f logs/catalina.out’
#qdbus org.kde.yakuake /yakuake/tabs org.kde.yakuake.setTabTitle $sess ‘tomcat’

# Function by link from the comments in page linked before - http://88.191.25.234/wordpress/2009/05/09/yakuake-launch-on-start/
function newTab() {
	sessionId="${1}"
	name="${2}"
	cmdString="${3}"

	qdbus org.kde.yakuake /yakuake/sessions addSession
	qdbus org.kde.yakuake /yakuake/tabs setTabTitle "${sessionId}" "${name}"
	if [ "${cmdString}" != "" ] ; then
		qdbus org.kde.yakuake /yakuake/sessions runCommandInTerminal "${sessionId}" "${cmdString}"
	fi
	# To take sshs time manipulate with tab name
	sleep 0.1
}

id=0
# Examples:
# newTab (( ++id)) tunnel
# newTab (( ++id)) mutt "mutt"
# newTab (( ++id)) apps "firefox &"
# newTab (( ++id)) irssi "irssi --connect=irc.freenode.net --nick=rpelisse"
# newTab (( ++id)) xradar "cd /perso/workspace/git-xradar/xradar"
# newTab (( ++id)) testproject "cd /perso/workspace/git-xradar/xradar/src/test/projects/testproject/"
# newTab (( ++id)) rss "snownews --update"
# newTab (( ++id)) mplayer "cd /perso/music/"
# newTab (( ++id)) nuxeo "cd /home/rpelisse/workspaces/nuxeo"

ssh-agent
ssh-add -l || ssh-add

# Start one instance, if needed
killall -0 yakuake || ( echo "Starting yakuake itself" && /usr/bin/yakuake && sleep 1 )

# Run command in initial tab
qdbus org.kde.yakuake /yakuake/sessions org.kde.yakuake.runCommand 'tmux attach || tmux'
qdbus org.kde.yakuake /yakuake/tabs org.kde.yakuake.setTabTitle 0 '|main'

newTab $(( ++id )) 'ROOT' 'sudo tmux'
